package org.example.dfs;

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/9/1 09:20
 * @apiNote
 */

public class Test00077Combine {

    public static void main(String[] args) {
        int n = 4;
        int k = 2;
        List<List<Integer>> res = combine(n,k);
        System.out.println(JSON.toJSON(res));
    }

    public static List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        int[] nums = new int[n];
        for(int i =0;i<n;i++){
            nums[i] = i+1;
        }
        dfs(nums,0,k,list,res);
        return res;
    }

    public static void dfs(int[] nums,int index,int k,List<Integer> list,List<List<Integer>> res){
        if(list.size() == k){
            res.add(new ArrayList<>(list));
            return;
        }
        for(int i = index;i<nums.length;i++){
            list.add(nums[i]);
            dfs(nums,i+1,k,list,res);
            list.remove(list.size()-1);
        }
    }


}
